/*
 * @Description: 
 * @Author: zoe
 * @Date: 2024-09-13 17:39:54
 * @LastEditors: rendc
 * @LastEditTime: 2024-09-13 18:36:50
 */
public class CounterClass {

    // 静态变量，用于记录实例化的次数
    private static int instanceCount = 0;

    // 构造方法，在每次实例化时增加计数器
    public CounterClass() {
        instanceCount++;
    }

    // 静态方法，用于获取当前实例化的次数
    public static int getInstanceCount() {
        return instanceCount;
    }

    // 示例方法，用于演示
    public void displayMessage() {
        System.out.println("Hello, this is an instance of CounterClass.");
    }

    // 主方法，用于测试
    public static void main(String[] args) {
        // 创建多个实例
        CounterClass obj1 = new CounterClass();
        CounterClass obj2 = new CounterClass();
        CounterClass obj3 = new CounterClass();

        // 获取并打印实例化的次数
        System.out.println("Total instances created: " + getInstanceCount());
    }
}
